package com.kpj.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.TextView;

public class SliderTestActivity extends Activity {
	private final static int 	DURATION = 500;
	
	private boolean				isAnimationInitialized;
	private boolean				isInAnimating;
	private boolean				isPopuped;
	private int					displayWidth;
	private int					displayHeight;
	private int					popupWidth;
	private int					popupHeight;
	private Animation 			popupAnimation;
	private Animation			dismissAnimation;
	
	private View 				popup;
	private TextView			popupContent;
	private Button 				popupButton;
	
	private int					contentId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slider);
		
		init();
	}
	
	private void init() {
		contentId = R.string.content_1_1;
		popup = findViewById(R.id.popup);
		popupContent = (TextView) findViewById(R.id.tv_popup_content);
		popup.setVisibility(View.INVISIBLE);
		popupContent.setVisibility(View.INVISIBLE);
		popupContent.setText(contentId);
		popupButton = (Button) findViewById(R.id.btn_slider_popup);
		popupButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isInAnimating)
					return;
				
				if (!isPopuped) {
					popupAnimationStart();
					isPopuped = true;
				} else {
					dismissAnimationStart();
					isPopuped = false;
				}
			}
			
		});
	}
	
	private void initSize() {
		initDisplaySize();
		initViewSize();
	}
	
	private void initDisplaySize() {
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		Display defaultDisplay = windowManager.getDefaultDisplay();
		displayWidth = defaultDisplay.getWidth();
		displayHeight = defaultDisplay.getHeight();
	}
	
	private void initViewSize() {
		popupWidth = popup.getWidth();
		popupHeight = popup.getHeight();
	}
	
	private void initAnimation() {
		popupAnimation = new ScaleAnimation(.1f, 1.f, .1f, 1.f);
		popupAnimation.setDuration(DURATION);
		popupAnimation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.bounce_interpolator));
		popupAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				popup.setVisibility(View.VISIBLE);
				popupContent.setVisibility(View.INVISIBLE);
				isInAnimating = true;
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				popupContent.setVisibility(View.VISIBLE);
				isInAnimating = false;
			}
		});
		
		dismissAnimation = new ScaleAnimation(1.f, .1f, 1.f, .1f);
		dismissAnimation.setDuration(DURATION);
		dismissAnimation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.bounce_interpolator));
		dismissAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				popupContent.setVisibility(View.INVISIBLE);
				isInAnimating = true;
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				popup.setVisibility(View.INVISIBLE);
				isInAnimating = false;
			}
		});
	}
	
	private void popupAnimationStart() {
		popup.startAnimation(popupAnimation);
	}
	
	private void dismissAnimationStart() {
		popup.startAnimation(dismissAnimation);
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if (!isAnimationInitialized) {
			initSize();
			initAnimation();
			isAnimationInitialized = true;
		}
		
		super.onWindowFocusChanged(hasFocus);
	}
}
